const koa = require('koa');
const app = new koa();

const Router = require('koa-router');
const router = new Router();

app.use((ctx, next) => {
  if (ctx.method === 'OPTIONS') {
    ctx.body = '';
  }
  next();
});

app.use((ctx, next) => {
  let origin = ctx.headers.origin;
  ctx.set('Access-Control-Allow-Origin', origin);
  ctx.set('Access-Control-Allow-Methods', 'GET');
  ctx.set('Access-Control-Allow-Headers', 'Authorization');
  next();
});

router.get('/user', async (ctx, next) => {
  ctx.body = {
    success: true
  };
});

app.use(router.routes());

app.listen(3000);
console.log('koa server is listening port 3000');
